<?php
if (!isset($_SESSION['nip'], $_SESSION['username'])) {
    header("Location: index.php?page=login");
}
?>

<?php include '_header.php'; ?>
<?php include '_menupanel.php'; ?>
<?php include '_menubar.php'; ?>

<script type="text/javascript">
    var url;
    var selectedKuliah;

    function accept() {
        var checked = [];
        var rows = $('#datagrid2').datagrid('getChecked');
        var item = rows.length > 1 ? "(" + rows.length + ") item yang terpilih" : rows[0].nim;
        for (var i = 0; i < rows.length; i++) {
            checked[i] = rows[i].id;
        }

        if (checked.length > 0) {
            $.messager.confirm('Confirm', 'Apakah Anda akan menerima pendaftaran <b>' + item + '</b>?', function(option) {
                if (option) {
                    $.ajax({
                        type: "POST",
                        url: "controller/kuliah_crud.php?act=approve",
                        data: 'id=' + JSON.stringify(checked),
                        success: function(data) {
                            $.messager.show({
                                title: 'Message',
                                msg: data,
                                timeout: 2000,
                                showType: 'slide'
                            });
                            $('#datagrid2').datagrid('reload');
                            $('#datagrid').datagrid('reload');
                        }
                    });
                }
            });
        }
    }
    function denied() {
        var checked = [];
        var rows = $('#datagrid2').datagrid('getChecked');
        var item = rows.length > 1 ? "(" + rows.length + ") item yang terpilih" : rows[0].nim;
        for (var i = 0; i < rows.length; i++) {
            checked[i] = rows[i].id;
        }
        
        if (checked.length > 0) {
            $.messager.confirm('Confirm', 'Apakah Anda akan menolak pendaftaran <b>' + item + '</b>?', function(option) {
                if (option) {
                    $.ajax({
                        type: "POST",
                        url: "controller/kuliah_crud.php?act=denie",
                        data: 'id=' + JSON.stringify(checked),
                        success: function(data) {
                            $.messager.show({
                                title: 'Message',
                                msg: data,
                                timeout: 2000,
                                showType: 'slide'
                            });
                            $('#datagrid2').datagrid('reload');
                        }
                    });
                }
            });
        }
    }

	function eliminate() {
        var checked = [];
        var rows = $('#datagrid').datagrid('getChecked');
        var item = rows.length > 1 ? "(" + rows.length + ") item yang terpilih " : rows[0].nim;
        for (var i = 0; i < rows.length; i++) {
            checked[i] = rows[i].id;
        }
        
        if (checked.length > 0) {
            $.messager.confirm('Confirm', 'Apakah Anda akan mengeluarkan <b>' + item + '</b> dari perkuliahan?', function(option) {
                if (option) {
                    $.ajax({
                        type: "POST",
                        url: "controller/kuliah_crud.php?act=eliminate",
                        data: 'id=' + selectedKuliah.id_det_matkul + '&id_mhs=' + JSON.stringify(checked),
                        success: function(data) {
							$.messager.show({
                                title: 'Message',
                                msg: data,
                                timeout: 2000,
                                showType: 'slide'
                            });
                            $('#datagrid').datagrid('reload');
                        }
                    });
                }
            });
        }
    }

    function refresh() {
        $('#datagrid').datagrid('load', {
            id_det_matkul: selectedKuliah.id_det_matkul
        });
    }
    function doSearch(value) {
        $('#datagrid').datagrid('load', {
            cari: value, id_det_matkul: selectedKuliah.id_det_matkul
        });
    }
    function refresh2() {
        $('#datagrid2').datagrid('load', {
            id_det_matkul: selectedKuliah.id_det_matkul
        });
    }
    function doSearch2(value) {
        $('#datagrid2').datagrid('load', {
            cari: value, id_det_matkul: selectedKuliah.id_det_matkul
        });
    }
    function formatItem(row) {
        var s = row.matkul + ' (<b>' + row.sks + '</b>)';
        return s;
    }

    $(document).ready(function() {
        
        $('#combo-kuliah').combobox({
            onSelect: function(data) {
                selectedKuliah = data;
                $('#datagrid').datagrid('load', {id_det_matkul: data.id_det_matkul});
                $('#datagrid2').datagrid('load', {id_det_matkul: data.id_det_matkul});
            }
        });

    });
</script>

<div class="pageContent">

    <div class="main">

        <div class="container">
            <div align="center">
                <h1>Data Peserta Perkuliahan WP-Learning</h1>
                <h2>Anda dapat melakukan manipulasi data peserta kuliah di halaman ini</h2>
            </div>
        </div>

        <div class="content" style="padding: 5px">
            <b>Mata Kuliah  :  </b>
            <input class="easyui-combobox" name="id_det_matkul" id="combo-kuliah" style="width: 200px;"
                   data-options="url:'controller/kuliah_crud.php?act=get_mykuliah',method:'get',
                   valueField:'id_det_matkul', textField:'matkul', panelHeight:'auto' "/>
        </div>

        <div id="tabs" class="content easyui-tabs">

            <div title="Mahasiswa Mendaftar">

                <div id="tbar2" style="padding:2px;height:30px;">
                    <div style="float:left;">
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" plain="true" onclick="accept()">Terima</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="denied()">Tolak</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="refresh2()">Refresh</a>
                    </div>
                    <div style="float: right; padding: 2px;">
                        <input id="cari_temp" class="easyui-searchbox" data-options="prompt:' Cari..', searcher:doSearch2" style="width:200px"></input> 
                    </div>
                </div>

                <table id="datagrid2" class="easyui-datagrid" url="controller/kuliah_crud.php?act=getunapproved" 
                       toolbar="#tbar" pagination="true" rownumbers="true" fitColumns="true" collapsible="true"
                       checkOnSelect="false" selectOnCheck="false" singleSelect="true" >
                    <thead>
                        <tr>
                            <th data-options="field:'ck',checkbox:true"></th>
                            <th data-options="field:'id'" hidden="true"></th>
                            <th data-options="field:'nim',width:70" sortable="true">NIM</th>
                            <th data-options="field:'nama',width:130" sortable="true">Nama</th>
                            <th data-options="field:'alamat',width:150" sortable="true">Alamat</th>
                            <th data-options="field:'telepon',width:70,align:'center'" sortable="true">Telepon</th>
                            <th data-options="field:'email',width:100,align:'center'" sortable="true">Email</th>
                            <th data-options="field:'tahun',width:50,align:'right'" sortable="true">Angkatan</th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div title="Mahasiswa Terdaftar">

                <div id="tbar" style="padding:2px;height:30px;">
                    <div style="float:left;">
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="refresh()">Refresh</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="eliminate()">Keluarkan</a>
                    </div>
                    <div style="float: right; padding: 2px;">
                        <input id="cari" class="easyui-searchbox" data-options="prompt:' Cari..', searcher:doSearch" style="width:200px"></input> 
                    </div>
                </div>

                <table id="datagrid" class="easyui-datagrid" url="controller/kuliah_crud.php?act=getjoined" 
                       toolbar="#tbar" pagination="true" rownumbers="true" fitColumns="true" collapsible="true"
                       checkOnSelect="false" selectOnCheck="false" singleSelect="true" >
                    <thead>
                        <tr>
                            <th data-options="field:'ck',checkbox:true"></th>
                            <th data-options="field:'id'" hidden="true"></th>
                            <th data-options="field:'nim',width:50" sortable="true">NIM</th>
                            <th data-options="field:'nama',width:130" sortable="true">Nama</th>
                            <th data-options="field:'alamat',width:170" sortable="true">Alamat</th>
                            <th data-options="field:'telepon',width:70,align:'center'" sortable="true">Telepon</th>
                            <th data-options="field:'email',width:100,align:'center'" sortable="true">Email</th>
                            <th data-options="field:'tahun',width:50,align:'right'" sortable="true">Angkatan</th>
                        </tr>
                    </thead>
                </table>

            </div>

        </div>

        <?php include '_footer.php'; ?>
    </div>
</div>

</body>
</html>